package org.wenhuayuan.project.main.http

import com.alibaba.android.arouter.launcher.ARouter
import org.wenhuayuan.library.resful.HiInterceptor
import org.wenhuayuan.library.resful.HiResponse
import org.wenhuayuan.project.common.router.ARouterPath

/**
 * 根据response 的 code 自动路由到相关页面
 */
open class HttpStatusIntercepter : HiInterceptor {
    override fun intercept(chain: HiInterceptor.Chain): Boolean {
        val response = chain.response()
        if (!chain.isRequestPeriod && response != null) {
            val code = response.code
            when (code) {
                HiResponse.RC_NEED_LOGIN -> ARouter.getInstance().build(ARouterPath.AROUTER_PATH_USER_LOGIN).navigation()
                // token 过期
                HiResponse.RC_AUTH_TOKEN_EXPIRED
                        or HiResponse.RC_AUTH_TOKEN_INVALID
                        or HiResponse.RC_USER_FORBID -> {
                    val helpUrl = response.errorData?.get("helpUrl")
                    ARouter.getInstance().
                        build(ARouterPath.AROUTER_DEGRADE_GLOBAL_ACTIVITY)
                        .withString("degrade_title", "非法访问")
                        .withString("degrade_desc", response.msg!!)
                        .withString("degrade_action", helpUrl)
                        .navigation()
                }

            }
        }

        return  false
    }

}